package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os/exec"
	"path/filepath"
	"strings"
)

func main() {
	// 定义命令行参数
	flag.Parse()
	filePaths := flag.Args()

	if len(filePaths) == 0 {
		log.Fatal("Please provide file paths as arguments")
	}

	// 将相对路径转换为绝对路径
	var absFilePaths []string
	for _, path := range filePaths {
		absPath, err := filepath.Abs(path)
		if err != nil {
			log.Fatalf("failed to get absolute path: %v", err)
		}
		absFilePaths = append(absFilePaths, absPath)
	}

	// 构建PowerShell命令
	files := strings.Join(absFilePaths, "\", \"")
	cmd := exec.Command("powershell", "-Command", fmt.Sprintf(`Add-Type -AssemblyName System.Windows.Forms; $data = New-Object System.Collections.Specialized.StringCollection; $data.AddRange(@("%s")); [System.Windows.Forms.Clipboard]::SetFileDropList($data)`, files))

	// 捕获标准错误输出
	stderr, err := cmd.StderrPipe()
	if err != nil {
		log.Fatalf("failed to get stderr pipe: %v", err)
	}

	// 启动命令
	if err := cmd.Start(); err != nil {
		log.Fatalf("failed to start command: %v", err)
	}

	// 读取错误输出
	errOutput, _ := ioutil.ReadAll(stderr)
	if err := cmd.Wait(); err != nil {
		log.Fatalf("command failed: %v, stderr: %s", err, errOutput)
	}

	fmt.Println("Files copied to clipboard:", absFilePaths)
}
